package game.module.quartz;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.DirectSchedulerFactory;
import org.quartz.simpl.RAMJobStore;
import org.quartz.simpl.SimpleThreadPool;
import org.quartz.spi.JobStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QuartzSchedulerManager {

	private static Logger logger = LoggerFactory.getLogger(QuartzSchedulerManager.class);

	static class SingletonHolder {
		static QuartzSchedulerManager instance = new QuartzSchedulerManager();
	}

	public static QuartzSchedulerManager getInstance() {
		return SingletonHolder.instance;
	}

	private Scheduler sched;

	private QuartzSchedulerManager() {
		try {
			// create the thread pool
			SimpleThreadPool threadPool = new SimpleThreadPool(2, Thread.NORM_PRIORITY);
			threadPool.setThreadNamePrefix("Quartz_Scheduler");
			threadPool.initialize();
			// create the job store
			JobStore jobStore = new RAMJobStore();
			DirectSchedulerFactory.getInstance().createScheduler("Quartz_Scheduler", "Quartz_Scheduler_Instance",
					threadPool, jobStore);
			sched = DirectSchedulerFactory.getInstance().getScheduler("Quartz_Scheduler");

			initJobs();

			sched.start();
		} catch (SchedulerException e) {
			logger.error("", e);
		}
	}

	private void initJobs() {
		// add job here
		// newJob();
		// 检查过期邮件
		checkPassDueMailJob();
		rankJob();
		// refreshStore();
		arenaReward();
//		pvpReward();
//		crossBossAnnounce();
//		rankPvp();
//		pvpRankReset();
//		craftWeekFreeHerosChange();
//		craftReward();
//		craftReset();
	}

	private void arenaReward() {
//		JobDetail jobDetail = JobBuilder.newJob(ArenaManager.class)
//				.withIdentity(ArenaManager.class.getSimpleName(), "group2").build();
//		Trigger trigger = TriggerBuilder.newTrigger()
//				.withIdentity(ArenaManager.class.getSimpleName() + "_trigger", "group2")
//				.withSchedule(CronScheduleBuilder.cronSchedule("0 0 22 ? * *")).build();
//		try {
//			sched.scheduleJob(jobDetail, trigger);
//		} catch (Exception e) {
//			logger.error("", e);
//		}
	}

	/**
	 * 每日凌晨5时：检查过期邮件
	 */
	private void checkPassDueMailJob() {
//		JobDetail jobDetail = JobBuilder.newJob(MailManager.class)
//				.withIdentity(MailManager.class.getSimpleName(), "group2").build();
//		Trigger trigger = TriggerBuilder.newTrigger()
//				.withIdentity(MailManager.class.getSimpleName() + "_trigger", "group2")
//				.withSchedule(CronScheduleBuilder.cronSchedule("0 0 5 * * ?")).build();
//		try {
//			sched.scheduleJob(jobDetail, trigger);
//		} catch (Exception e) {
//			logger.error("", e);
//		}
	}

	/**
	 * 排行榜
	 */
	private void rankJob() {
//		JobDetail jobDetail = JobBuilder.newJob(RankManager.class)
//				.withIdentity(RankManager.class.getSimpleName(), "group2").build();
//		Trigger trigger = TriggerBuilder.newTrigger()
//				.withIdentity(RankManager.class.getSimpleName() + "_trigger", "group2")
//				.withSchedule(CronScheduleBuilder.cronSchedule("0 30 5 ? * *")).build();
//		try {
//			sched.scheduleJob(jobDetail, trigger);
//		} catch (Exception e) {
//			logger.error("", e);
//		}
	}

	public void shutdown() {
		try {
			sched.shutdown(true);
		} catch (SchedulerException e) {
			logger.error("", e);
		}
	}

}
